home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2000 #4 / Amiga Plus CD - 2000 - No. 4.iso / c / lavd.c < prev    next >
C/C++ Source or Header  |  2000-05-27  |  2KB  |  121 lines

  1. #include <exec/exec.h>
  2. #include <clib/exec_protos.h>
  3. #include <clib/timer_protos.h>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7.  
  8. #include "lavd.h"
  9. #include "timer.h"
  10.  
  11. struct ExecBase    *ExecBase;
  12. struct MsgPort *RequestPort = NULL;
  13. static char PortName[] = "lavd";
  14.  
  15. main()
  16. {
  17.     lavdMsg    *Mesg;
  18.     int    Stop = 0;
  19.     int    L1 = 0, L2 = 0, L3 = 0;
  20.     struct timeval    Start;    /* Startup time. */
  21.     struct timeval    Now;    /* Current time. */
  22.  
  23.     ExecBase = OpenLibrary("exec.library", 36L);
  24.  
  25.     if (!timer_open()){
  26.         PutStr("Cannot open timer.\n");
  27.         goto exitmain;
  28.     }
  29.     if ((RequestPort = CreateMsgPort()) == NULL) {
  30.         PutStr("Cannot create message port.\n");
  31.         goto exitmain;
  32.     }
  33.  
  34.     /*
  35.      * This is a critical section - if a lavd process is adding it's
  36.      * port, another cannot also do so at the same time, because
  37.      * neither will see the others port in the FindPort, and so two
  38.      * will end up running at once.
  39.      */
  40.  
  41.     Forbid();
  42.     if (FindPort(PortName)) {
  43.         Permit();
  44.         PutStr("Already running.\n");
  45.         goto exitmain;
  46.     }
  47.     RequestPort->mp_Node.ln_Name = "lavd";
  48.     RequestPort->mp_Node.ln_Pri = 0;
  49.     AddPort(RequestPort);
  50.  
  51.     Permit();
  52.  
  53.     GetSysTime(&Start);
  54.  
  55.     while (! Stop){
  56.         ULONG    Sig;
  57.  
  58.         timer_start (250000);    /* 1/4 of a second. */
  59.         Sig = Wait(1L<<RequestPort->mp_SigBit | 1L<<timer_port->mp_SigBit);
  60.         if (Sig & 1L<<RequestPort->mp_SigBit){
  61.             Forbid();
  62.             while ((Mesg = (lavdMsg *) GetMsg (RequestPort)) != NULL){
  63.                 switch(Mesg->Class){
  64.                     case LAV_STOP:
  65.                         Stop = 1;
  66.                         break;
  67.                            case LAV_LOAD: 
  68.                         Mesg->L1 = L1 / 1000;
  69.                         break;
  70.                     case LAV_ALL:
  71.                         GetSysTime (&Now);
  72.                         Mesg->Uptime = Now.tv_secs - Start.tv_secs;
  73.                         /* Fall though \/ */
  74.                         case LAV_LOADS:
  75.                         Mesg->L1 = L1 / 1000;
  76.                         Mesg->L2 = L2 / 1000;
  77.                         Mesg->L3 = L3 / 1000;
  78.                         break;
  79.                         case LAV_UP:
  80.                         GetSysTime (&Now);
  81.                         Mesg->Uptime = Now.tv_secs - Start.tv_secs;
  82.                         break;
  83.                     default:
  84.                         break;
  85.                 }
  86.                 ReplyMsg(Mesg);
  87.             }
  88.             Permit();
  89.         } 
  90.         if (Sig & 1L<<timer_port->mp_SigBit){
  91.             int    Count = 0;
  92.             struct Node    *node;
  93.  
  94.             Disable();
  95.             for (node = ExecBase->TaskReady.lh_Head; node->ln_Succ; node = node->ln_Succ){
  96.                 Count++;
  97.             }
  98.             Enable();
  99.             Count *= 100000;
  100.             L1 *= 120;
  101.             L1 += Count;
  102.             L1 /= 121;
  103.             L2 *= 1200;
  104.             L2 += Count;
  105.             L2 /= 1201;
  106.             L3 *= 3600;
  107.             L3 += Count;
  108.             L3 /= 3601;
  109.         }
  110.     }
  111.  
  112.     RemPort(RequestPort);
  113.       exitmain:
  114.     timer_close();
  115.     if (RequestPort) {
  116.         DeleteMsgPort(RequestPort);
  117.     }
  118.     CloseLibrary(ExecBase);
  119.     exit(0);
  120. }
  121.